﻿using BaseDll;
using CommunityToolkit.Mvvm.ComponentModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace DisplayReading.Model
{
    /// <summary>
    /// 
    /// </summary>
    public class ucDispalyData:ObservableObject
    {
      
        /// <summary>
        /// 是否转发
        /// </summary>
        private bool _isSend;
        /// <summary>
        /// 是否转发
        /// </summary>
        public bool isSend { get => _isSend; set => SetProperty(ref _isSend, value); }
        /// <summary>
        /// 本地ip
        /// </summary>
        private string _locahostIp;
        /// <summary>
        /// 本地ip
        /// </summary>
        public string locahostIp { get => _locahostIp; set => SetProperty(ref _locahostIp, value); }
        /// <summary>
        /// 本地预览
        /// </summary>
        private bool _preview;
        /// <summary>
        /// 本地预览
        /// </summary>
        public bool preview { get => _preview; set => SetProperty(ref _preview, value); }
        /// <summary>
        /// 帧数
        /// </summary>
        private int _fps;
        /// <summary>
        /// 帧数
        /// </summary>
        public int fps { get => _fps; set => SetProperty(ref _fps, value); }


        /// <summary>
        /// 发送地址
        /// </summary>
        private List<SendClient> _sendClient;
        /// <summary>
        /// 发送地址
        /// </summary>
        public List<SendClient> sendClient { get => _sendClient; set => SetProperty(ref _sendClient, value); }


        /// <summary>
        /// 显示器列表
        /// </summary>
        private List<DisplayData> _displayList;
        /// <summary>
        /// 显示器列表
        /// </summary>
        public List<DisplayData> displayList { get => _displayList; set => SetProperty(ref _displayList, value); }

        /// <summary>
        /// 配置
        /// </summary>
        private FileConfig _config;
        /// <summary>
        /// 配置
        /// </summary>
        public FileConfig config { get => _config; set => SetProperty(ref _config, value); }

        public SendServer sendServer { get; set; }

        public ucDispalyData()
        {
            config= FileDataClass.Get<FileConfig>();
            sendServer = new SendServer();
            sendServer.port = config._sendPort;
            sendServer.userChaneg += SendServer_userChaneg;
            sendServer.TcpData += SendServer_TcpData;
            fps = config.fps;
           
        }

        private void SendServer_TcpData(TcpClient tcp,string obj)
        {
            var data= JsonHelp.ToObj<ComData>(obj);
            if (data.com==EnumCom.ini)
            {
                ComData com = new ComData()
                {
                    com = EnumCom.ini,
                    dataCount = sendServer.serverCount,
                    fps = fps,
                    startPort = config._sendPort+1
                };
                sendServer.TcpSend(tcp,JsonHelp.ToString(com));
            }
            if (data.com == EnumCom.heart)
            {
                sendServer.TcpSend(tcp,obj);
            }
        }

        public void ServerStart(int count)
        {
            sendServer.serverCount = count;
            sendServer.Start();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        private void SendServer_userChaneg(Dictionary<nint, string> obj)
        {
            if (sendClient==null)
            {
                sendClient=new List<SendClient>();
            }
            List<SendClient> user = new List<SendClient>();
            foreach (var item in obj)
            {
                SendClient sc = new SendClient()
                {
                    ip=item.Value,
                    connid= item.Key
                };
                user.Add(sc);
                var olduser = sendClient.Find(x => x.connid == item.Key);
                if (olduser!=null&& olduser.isSend)
                {
                    sc.isSend = true;
                }
            }
            sendClient = user;
        }

        public void Clear()
        {
            if (sendClient!=null)
            {
                sendClient.Clear();
            }
            config.fps = fps;
            FileDataClass.Save(config);
            sendServer.Close();
        }
    }
}
